#!/usr/bin/env python
""" AnyBackup is a windows specific backup program to keep multiple volumes in sync with multiple volumes
    Copyright (C) 2011  Andrew Hopkinson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Exported Classes:

    messagingMixin -- a small mixin to support setting and writing to a messaging queue
    pathMixin -- provides a path building function, for reasons unknown os.path.join sometimes returns nonsensical results
    """

import os
import time

def print_timing(func):
    def wrapper(*arg):
        t1 = time.time()
        res = func(*arg)
        t2 = time.time()
        print '%s took %0.3f ms' % (func.func_name, (t2 - t1) * 1000.0)
        return res

    return wrapper

class messagingMixin:
        def setMessagingQueue(self,queue):
                global messagingQueue
                messagingQueue = queue
                
        def sendMessage(self,message):
                if messagingQueue:
                        messagingQueue.put(message,1)
                else:
                        print message
                        
class pathMixin:
        def toFullPath(self,path):
                path = path.replace('\\\\','\\')
                return u'\\\\?\\'+path
        
        def fullPath(self,*args):
                return u'\\\\?\\'+self.createPath(*args)
        
        def createPath(self,*args):
                pathStr = u''
                for piece in args:
                        if pathStr:
                                pathStr += os.sep
                        pathStr += piece
                while '\\\\' in pathStr:
                        pathStr = pathStr.replace('\\\\','\\')
                return pathStr
                
                